package com.ocom.payconfig.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.card.EntityConsTypeConfig;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.dto.ConsTypeConfigDto;
import com.ocom.common.response.web.GetConsTypeResponse;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoConsTypeConfig;
import com.ocom.payconfig.service.ConsTypeConfigService;
import com.ocom.redis.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ConsTypeConfigServiceImpl extends ServiceImpl<IDaoConsTypeConfig, EntityConsTypeConfig> implements ConsTypeConfigService {

	@Autowired
	private IDaoConsTypeConfig iDaoConsTypeConfig;

	@Resource
	private RedisUtil redisUtil;

	@Override
	@DataSourceAspect(type = DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
	public List<GetConsTypeResponse> getListByComId(Long companyId) {
		List<GetConsTypeResponse> list = iDaoConsTypeConfig.getConsType(companyId);
		if(list==null || list.size()==0){
			list=new ArrayList<>();
			EntityConsTypeConfig entityConsTypeConfig = new EntityConsTypeConfig();
			entityConsTypeConfig.setComId(companyId);
			entityConsTypeConfig.setConsType(1);
			entityConsTypeConfig.setConsName("饭堂");
			entityConsTypeConfig.setWalletSerial("12");
			entityConsTypeConfig.setWithholdSerial(0);
			entityConsTypeConfig.setCreateUser("admin");
			boolean save = this.save(entityConsTypeConfig);

			if (save) {
				GetConsTypeResponse getConsTypeResponse = JSON.parseObject(JSON.toJSONString(entityConsTypeConfig), GetConsTypeResponse.class);
				list.add(getConsTypeResponse);
			}

			EntityConsTypeConfig entityConsTypeConfig1 = new EntityConsTypeConfig();
			entityConsTypeConfig1.setComId(companyId);
			entityConsTypeConfig1.setConsType(2);
			entityConsTypeConfig1.setConsName("商店");
			entityConsTypeConfig1.setWalletSerial("12");
			entityConsTypeConfig1.setWithholdSerial(0);
			entityConsTypeConfig1.setCreateUser("admin");
			boolean save1 = this.save(entityConsTypeConfig1);
			if (save1) {
				GetConsTypeResponse getConsTypeResponse = JSON.parseObject(JSON.toJSONString(entityConsTypeConfig1), GetConsTypeResponse.class);
				list.add(getConsTypeResponse);
			}

		}
		return  list;
	}

	@DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.MASTER)
	@Override
	public EntityConsTypeConfig selectConsTypeName(Long comId, String consName, Integer consType) {
		QueryWrapper<EntityConsTypeConfig> queryWrapper=new QueryWrapper<>();
		queryWrapper.eq("com_id" , comId);
		queryWrapper.eq("cons_name" , consName);
		queryWrapper.ne("cons_type" , consType);
		queryWrapper.last("limit 1");
		return iDaoConsTypeConfig.selectOne(queryWrapper);
	}

	@DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "companyId",isMaster = MasterSlaveEnum.MASTER)
	@Override
	public boolean updateConsType(Long companyId, ConsTypeConfigDto entityConsTypeConfig1) {
		redisUtil.del("cons_type_config#comId:"+companyId+"#consType:"+entityConsTypeConfig1.getConsType()+"#");
		EntityConsTypeConfig entityConsTypeConfig = (EntityConsTypeConfig) entityConsTypeConfig1;
		entityConsTypeConfig.setUpdateTime(new Date());
		UpdateWrapper<EntityConsTypeConfig> updateWrapper=new UpdateWrapper<EntityConsTypeConfig>();
		updateWrapper.eq("com_id",entityConsTypeConfig.getComId());
		updateWrapper.eq("cons_type",entityConsTypeConfig.getConsType());
		return this.update(entityConsTypeConfig,updateWrapper);
	}

	@DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.MASTER)
	@Override
	public EntityConsTypeConfig selectXFConsTypeName(Long comId, Integer consType) {
		QueryWrapper<EntityConsTypeConfig> queryWrapper=new QueryWrapper<>();
		queryWrapper.eq("com_id" , comId);
		queryWrapper.eq("cons_type" , consType);
		queryWrapper.orderByDesc("update_time");
		queryWrapper.last("limit 1");
		return iDaoConsTypeConfig.selectOne(queryWrapper);
	}
}
